package tools;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.sf.json.JSONArray;

import com.dragonlance.beans.BuffTemplate;
import com.dragonlance.beans.ItemTemplate;
import com.dragonlance.beans.Skill;

public class TemplatesGenerator {

	/**
	 * @param args
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	public static void main(String[] args){
		String dataPath = "E:/flashworkspace/webdragonlance/Startup/bin-debug/data/";
		try {
			generate(dataPath+"物品模板表.xls", dataPath+"itemtemplate.json", ItemTemplate.class);
			generate(dataPath+"buff模板表.xls", dataPath+"bufftemplate.json", BuffTemplate.class);
			generate(dataPath+"技能模板表.xls", dataPath+"skilltemplate.json", Skill.class);
			System.out.println("物品模板表生成成功!");
		} catch (BiffException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void generate(String source,String target, Class<?> templateBean) throws NoSuchMethodException, SecurityException, NumberFormatException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException, BiffException, InstantiationException{
		File f = new File(source);
		Workbook workbook = Workbook.getWorkbook(f);
		Sheet sheet = workbook.getSheet(0);
		int rows = sheet.getRows();
		int cols = sheet.getColumns();
		Constructor<?> constructor = templateBean.getConstructor();
		List<Object> templates = new ArrayList<Object>();
		for(int i=1;i<rows;i++){
			Object item = constructor.newInstance();
			for(int j=0;j<cols;j++)
			{
				String field = sheet.getCell(j,0).getContents();
				char head = field.charAt(0);
				field = field.substring(1, field.length());
				head = Character.toUpperCase(head);
				String getMethodName = "get"+head+field;
				String setMethodName = "set"+head+field;
				Method getMethod = templateBean.getMethod(getMethodName);
				Method setMethod;
				Class<?> p = getMethod.getReturnType();
				if(p.getName()=="int"){
					setMethod = templateBean.getMethod(setMethodName,int.class);
					setMethod.invoke(item, Integer.parseInt(sheet.getCell(j,i).getContents()));
				}else if(p.getName()=="java.lang.String")
				{
					setMethod = templateBean.getMethod(setMethodName,String.class);
					setMethod.invoke(item, sheet.getCell(j,i).getContents());
				}else if(p.getName()=="double")
				{
					setMethod = templateBean.getMethod(setMethodName,double.class);
					setMethod.invoke(item, Double.parseDouble(sheet.getCell(j,i).getContents()));
				}else if(p.getName()=="boolean")
				{
					setMethod = templateBean.getMethod(setMethodName,boolean.class);
					setMethod.invoke(item, Boolean.parseBoolean(sheet.getCell(j,i).getContents()));
				}
			}
			templates.add(item);
		}
		JSONArray ja = JSONArray.fromObject(templates);
		File targetFile = new File(target);
		if(!targetFile.exists())//如果文件不存在,则新建.
		{
		    File parentDir=new File(targetFile.getParent());
		    if(!parentDir.exists())//如果所在目录不存在,则新建.
		    	parentDir.mkdirs();
		    targetFile.createNewFile();
		}
		PrintWriter pw = new PrintWriter(new  FileWriter(target),true);
		pw.println(ja.toString());
		pw.close();
	}
}
